<?php
/**
 * @software         [JiJinCMS]
 * @author           [上海集锦信息科技有限公司]
 * @website          [http://www.jijinweb.com]
 * @email            [sunny@jijinweb.com]
 **/
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class Feedback extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\FeedbackModel();
        $this->validate=new \app\admin\validate\FeedbackValidate();
        View::assign('cat', Db::name("feedback_cat")->order("sort asc,catid asc")->select());
    }    
    //
    public function index(){
        $where=[];
        if(Request::param("catid")){
            $where["catid"]=Request::param("catid",0,"intval");
        }
        //查询与分页
        $listRows=Request::param("listRow")?Request::param("listRow"):20;//每页显示多少条
        $data=$this->model->where($where)->order("is_read asc,addtime desc")->paginate(['list_rows'=>$listRows,'query' => request()->param()]);
        foreach($data as $key=>$v){
            $data[$key]['cat']=Db::name("feedback_cat")->find($v['catid']);
        }
        // 获取分页显示
        $pagelist = $data->render();        
        View::assign("pagelist",$pagelist);
        View::assign('data', $data);
        return View::fetch();
    }
    //信息展示
    public function view(){        
        View::assign('详情', $data);
        $id=Request::param("id",0,"intval");
        $r=$this->model->find($id);        
        if(!$r){
            $this->error("信息不存在");
        }
        $cat=Db::name("feedback_cat")->find($r['catid']);
        $r['cat']=$cat;
        //设置为已读
        $seetdata["id"]=$id;
        $seetdata["is_read"]=1;
        $this->model->update($seetdata);
        //
        $formtemp=$cat["formtemp"];
        $formtemp=explode("\r\n",$formtemp);
        foreach($formtemp as $key=>$v){            
            $val=explode("<!--field-->",$v);         
            //判断是否为录入字段
            if(!strstr($cat["is_enter"],",".$val[1].",")){
                continue;
            }
            $field=Db::name("feedback_field")->where(array("field"=>$val[1]))->find();
            $newdata[$key]["name"]=$val[0];
            $newdata[$key]["field"]=$val[1];
            $newdata[$key]["value"]=$r[$val[1]];
            $newdata[$key]["defaultval"]=$field["defaultval"];
            $newdata[$key]["formtype"]=$field["formtype"];
            //
        }
        View::assign('r', $r);
        View::assign('data', $newdata);
        return View::fetch();
    }
    //
    public function delete(){
        $ids=Request::param("ids",0,"intval");
        if(!$ids){
             $this->error=('请选择信息！');
        }
        foreach($ids as $id){
            $this->model->destroy($id);
        } 
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$ids,$this->request->param());  
        $this->success("删除成功");
    }
    public function setRead(){
        $ids=Request::param("ids",0,"intval");
        if(!$ids){
             $this->error('请选择信息！');
        }
        foreach($ids as $id){
            $this->model->update([
                'id'=>$id,
                'is_read'=>1,
            ]);
        } 
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$ids,$this->request->param());  
        $this->success("操作成功");
    }   
    public function export(){
        $where=[];
        $catid=Request::param("catid");
        if(!$catid){
            $this->error('缺少catid！');
        }
        $cat=Db::name("feedback_cat")->find($catid);
        
        $where["catid"]=$catid;
        $data=$this->model->where($where)->order("is_read asc,addtime desc")->select();
        
        $formtemp=$cat["formtemp"];
        $formtemp=explode("\r\n",$formtemp);
        $fieldInfo=[];
        foreach($formtemp as $key=>$v){            
            $val=explode("<!--field-->",$v);         
            //判断是否为录入字段
            if(!strstr($cat["is_enter"],",".$val[1].",")){
                continue;
            }
            $field=Db::name("feedback_field")->where("field",$val[1])->find();
            $fieldInfo[]=[
                "name"=>$val[0],
                "field"=>$val[1],
                "formtype"=>$field['formtype'],
                "defaultval"=>$field['defaultval'],
            ];
        }
        $objPHPExcel = new \PHPExcel();
        //接下来就是写数据到表格里面去  
        $objActSheet = $objPHPExcel->getActiveSheet(0);  
        //表头
        foreach($fieldInfo as $k=>$v){
            $objActSheet->setCellValue(IntToChr($k).'1',$v['name']);
        }
        //内容
        foreach($data as $k1=>$v){
            foreach($fieldInfo as $k2=>$fi){
                if(!$v[$fi["field"]]){
                    $value="-";
                }else if($fi["formtype"]=="date"){
                    $value=date("Y-m-d H:i:s",$v[$fi["field"]]);
                }else if($fi["formtype"]=="morevalue"){
                    $morevalue_str='';
                    //列名
                    $morevalueField=str2arr($fi['defaultval'],"{{RsSplit}}","{{FieldSplit}}");
                    foreach($morevalueField as $v2){     
                        $morevalue_str.=$v2[0]."    ";
                    }
                    //内容
                    $morevalue=str2arr($v[$fi["field"]],"{{RsSplit}}","{{FieldSplit}}");
                    foreach($morevalue as $v2){
                        $morevalue_str.="\r\n";
                        foreach($morevalueField as $k3=>$v3){
                            $morevalue_str.=$v2[$k3].'  ';
                        }
                    }
                    $value=$morevalue_str;
                }else{
                    $value=$v[$fi["field"]];
                }
                $objActSheet->setCellValue(IntToChr($k2).($k1+2),$value);
                $objActSheet->getStyle(IntToChr($k2).($k1+2))->getAlignment()->setWrapText(true);
            }
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="信息反馈-'.$cat['name'].'.xls"');
        header("Content-Disposition:attachment;filename=信息反馈-".$cat['name'].".xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');    
    }    
}
